<?php
require_once 'CassandraConnection.php';

class BaseDao {
	private $_conn;

	public function __construct() {
		$this->_conn = CassandraConnection::singleton('127.0.0.1', '9160');
	}

	public function getColumnFamilyRows($columnFamily, $limit = 100, &$offset = '', $columnsLimit = null) {
		$keyRange = new cassandra_KeyRange();
		if (trim($offset) == '') {
			$keyRange->start_key = '';
			$keyRange->end_key = '';
		} else {
			$keyRange->start_token = $offset;
			$keyRange->end_token = '';
		}
		$keyRange->count = $limit;

		$sliceRange = new cassandra_SliceRange();
		$sliceRange->start = '';
	  	$sliceRange->finish = '';
	  	if (isset($columnsLimit)) {
	  		$sliceRange->count = $columnsLimit;
	  	}
	  	$predicate = new cassandra_SlicePredicate();
	  	$predicate->slice_range = $sliceRange;

	  	$slices = $this->_conn->getClient()->get_range_slices($columnFamily->getKeyspace(), $columnFamily->getColumnParent(), $predicate, $keyRange, PandraCore::getConsistency());
		return self::parseColumnsRangeSlicesIntoArray($slices);
	}

	private static function parseColumnsRangeSlicesIntoArray($slices) {
		$res = array();
		foreach ($slices as $slice) {
			$colsArray = array();
			foreach ($slice->columns as $column_or_supercolumn) {
				$colsArray[$column_or_supercolumn->column->name] = $column_or_supercolumn->column->value;
			}
			$res[$slice->key] = $colsArray;
		}
		return $res;
	}
}